//
//  ScanVC.swift
//  DianXia
//
//  Created by you&me on 2017/12/18.
//  Copyright © 2017年 you&me. All rights reserved.
//

let ScreenWidth: CGFloat = UIScreen.main.bounds.size.width
let ScreenHeight: CGFloat = UIScreen.main.bounds.size.height
let ScreenBounds: CGRect = UIScreen.main.bounds

import Foundation

class ScanVC: LBXScanViewController {

    let dispose = DisposeBag()
    var eventType = HomeEventType.Repair

    lazy var scBackBtn: UIButton = {
        let btn = UIButton()
        btn.frame = CGRect(x: ScreenWidth - 60, y: 21, width: 53, height: 33)
       // btn.setImage(UIImage(named: "下拉"), for: .normal)
        btn.setTitle("返回", for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.backgroundColor = UIColor.lightGray
        return btn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()



        //需要识别后的图像
        setNeedCodeImage(needCodeImg: true)
        //框向上移动10个像素
        scanStyle?.centerUpOffset += 10



    }

    override func viewDidAppear(_ animated: Bool) {

        super.viewDidAppear(animated)
        
        self.view.addSubview(scBackBtn)
        scBackBtn.rx.tap
            .subscribe({[weak self] _ in self?.dismiss(animated: true, completion: nil) })
            .disposed(by: dispose)
    }
    override func handleCodeResult(arrayResult: [LBXScanResult]) {

        for result:LBXScanResult in arrayResult
        {
            if let str = result.strScanned {
                print(str)
            }
        }

        let result:LBXScanResult = arrayResult[0]
        print("二维码扫描结果\(result.strScanned ?? "")")
        if let resultStr = result.strScanned {
            let url = URLComponents(string: resultStr)
            let code =  url?.queryItems![0].value
            self.isInMyTasks(code ?? "")
        }
    }

    fileprivate func isInMyTasks( _ code: String) {

        var codeArray = [String]()
        var dic = [String: Int]()

        for task in UserActivingTasks.share.tasks {
            codeArray.append(task.code ?? "")
            dic[task.code ?? ""] = task.no ?? 0
        }


        if codeArray.contains("112233") {
            //如果包含在内
            switch eventType {
            case .Repair:
                print("维修")
                let suVC = UIStoryboard.viewController(from: .Home, viewControllerClass: RepairVC.self)
                suVC.code = code
                suVC.taskNo = dic[code] ?? 0
                suVC.vcType = .Repair
                self.present(suVC, animated: true, completion: nil)
            case .Support:
                print("补货")
                let suVC = UIStoryboard.viewController(from: .Home, viewControllerClass: SupportVC.self)
                suVC.code = code
                suVC.taskNo = dic[code] ?? 0
                self.present(suVC, animated: true, completion: nil)
            case .Active:
                let activeVC = UIStoryboard.viewController(from: .Home, viewControllerClass: DredgeVC.self)
                activeVC.code = code
                self.present(activeVC, animated: true, completion: nil)
                print("开通")
            case .Unlive:
                print("退服")
                let suVC = UIStoryboard.viewController(from: .Home, viewControllerClass: RepairVC.self)
                suVC.code = code
                suVC.taskNo = dic[code] ?? 0
                suVC.vcType = .Unlive
                self.present(suVC, animated: true, completion: nil)
            }

        }else{
            BCShowMessage.shareMessage.showTipMsg("对不起，该售货机不在您的任务列表内！", time: 1)
            self.dismiss(animated: true, completion: nil)
        }
    }

}



